<script setup>
import {onMounted, reactive, ref} from "vue";
import router from "@/router/index.js";
import {deleteMessageService, messagePageListService} from "@/api/message.js";
import {ElMessage, ElMessageBox} from "element-plus";

const params = reactive({
  current: 1,
  size: 5,
})
const totalSize = ref(0)

function goBack() {
  router.go(-1)
}

const messageList = ref([])

async function getMessageList() {
  const result = await messagePageListService(params)
  messageList.value = result.data.records
  totalSize.value = result.data.total
}

onMounted(() => {
  getMessageList()
})

function handleSizeChange() {
  params.current = 1
  getMessageList()
}

function handleCurrentChange() {
  getMessageList()
}

async function handlerDelete(id) {
  try {
    await ElMessageBox.confirm(
        '确定要删除该消息吗?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )
    await deleteMessageService(id)
    ElMessage.success("删除该消息成功!")
    await getMessageList()
  } catch (error) {
    if (error === "cancel") {
    }
  }
}
</script>

<template>
  <div class="message-list">
    <div class="message-list-block">
      <div class="message-list-header">
        <span class="ms-title">我的消息</span>
        <span class="message-list-back" @click="goBack()">返回</span>
      </div>
      <div class="mesage-list-content" v-for="(item, index) in messageList" :key="index">
        <img src="@/assets/images/通知.png" width="50px" class="image"/>
        <div class="message-content">
          <div class="title-box">
            <div class="title">
              {{ item.title }}
            </div>
          </div>
          <div class="content">{{ item.content }}</div>
        </div>
        <el-button class="mesage-button" type="text" @click.stop="handlerDelete(item.id)">删除</el-button>
      </div>
      <div class="message-pagination">
        <!-- 增加分页展示器 -->
        <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                       :total="totalSize"
                       v-model:current-page="params.current"
                       v-model:page-size="params.size"
                       :page-sizes="[5, 10, 15, 20]"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"/>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.message-list {
  background-color: #ffffff;
  position: relative;
  overflow: hidden;
  display: flex;
  justify-content: center;
  margin-top: 20px;

  .message-list-block {
    margin: 0 auto;
    margin-bottom: 1px;
    margin-top: 20px;
    width: 100%;
    max-width: 1520px;

    .message-pagination {
      margin: 20px 0 0 0;
      padding-top: 20px;
      border-top: 1px solid #f0f0f0; /* 添加分割线 */
      display: flex;
      justify-content: flex-end;
    }

    .message-list-header {
      background-color: rgba(250, 250, 250, 1);
      border-radius: 10px;
      width: 100%;
      height: 60px;
      font-size: 20px;
      text-indent: 20px;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.05);

      .ms-title {
        font-family: PingFangSC, PingFang SC;
        font-weight: 600;
        font-size: 18px;
        color: #222222;
        text-align: left;
      }

      .message-list-back {
        cursor: pointer;
        color: #666666;
        font-size: 18px;
        margin-left: auto;
        padding-right: 30px;
        transition: color 0.3s ease;

        &:hover {
          color: #222222;
        }
      }
    }

    .mesage-list-content {
      box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.1);
      background-color: rgba(255, 255, 255, 1);
      border-radius: 10px;
      margin-top: 20px;
      max-width: 1520px;
      background: #FFFFFF;
      box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.1);
      border-radius: 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 110px;
      width: 100%;
      padding-left: 20px;

      &:hover {
        box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
        transform: translateY(-2px);
        transition: all 0.3s ease;
      }

      .mesage-button {
        width: 84px;
        height: 44px;
        background: #EAF9FF;
        border-radius: 22px;
        margin-right: 20px;
      }

      .message-content {
        color: #333333;
        position: relative;
        left: 20px;
        width: calc(100% - 155px);

        .title-box {
          display: flex;
          align-items: center;
          margin-bottom: 10px;
        }

        .title {
          font-weight: bold;
          font-size: 16px;
        }
      }
    }
  }
}
</style>